#include "vpxdecoder.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "vpx/vpx_decoder.h"

#include "vpx_component/tools_common.h"
#include "vpx_component/video_reader.h"
//#include "vpx_component/vpx_config.h"
#include <QDebug>

VPxDecoder::VPxDecoder(QWidget *parent) : QOpenGLWidget(parent)
{   

}
VPxDecoder::~VPxDecoder()

{
    if (_rgbBuffer)
    {
        delete [] _rgbBuffer;
        _rgbBuffer = Q_NULLPTR;
    }
}
int VPxDecoder::decode_(int pox,int poy,int imageWidth,int imageHeight,QByteArray *in) {
    QByteArray out ;
    int frame_cnt = 0;
    vpx_codec_ctx_t codec;
    VpxVideoReader *reader = NULL;
    const VpxInterface *decoder = NULL;
    const VpxVideoInfo *info = NULL;

    reader = vpx_video_reader_open(in);

    info = vpx_video_reader_get_info(reader);

    decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
    if (!decoder) die("Unknown input codec.");
    if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
        die("Failed to initialize decoder.");
    while (vpx_video_reader_read_frame(reader)) {
        vpx_codec_iter_t iter = NULL;
        vpx_image_t *img = NULL;
        size_t frame_size = 0;
        const unsigned char *frame =
                vpx_video_reader_get_frame(reader, &frame_size);
        if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
            die_codec(&codec, "Failed to decode frame.");
        while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
            vpx_img_write(img, &out);
            //转换图片
            ConvertARPG(pox,poy,imageWidth,imageHeight,out);
            ++frame_cnt;
        }
    }

    //关闭
    if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
    vpx_video_reader_close(reader);
    //delete out;
    return EXIT_SUCCESS;
}
void VPxDecoder::ConvertARPG(int pox,int poy,int imageWidth,int imageHeight,QByteArray& data)
{
    int data_length = imageWidth * imageHeight * 4;

    if (data_length == 0)
    {
        qFatal("数据长度不对");
        return;
    }
    if (_data_length != data_length)
    {
        _data_length = data_length;
        if (_rgbBuffer)
        {
            delete [] _rgbBuffer;
            _rgbBuffer = Q_NULLPTR;
        }
    }
    if (_rgbBuffer == Q_NULLPTR)
    {
        _rgbBuffer = new uchar [_data_length];
    }
    //旋转
    int dest_stride_Y = imageWidth;
    int dest_stride_U = imageWidth / 2;
    int dest_stride_V = imageWidth / 2;
    const char  *Y_data_dest = data.constData();
    const char  *U_data_dest = Y_data_dest + imageWidth * imageHeight;
    const char  *V_data_dest = U_data_dest + imageWidth * imageHeight / 4;
    //转ARGB
    libyuv::I420ToARGB((uint8_t*)Y_data_dest, dest_stride_Y,
                       (uint8_t*)U_data_dest, dest_stride_U,
                       (uint8_t*)V_data_dest, dest_stride_V,
                       _rgbBuffer, imageWidth * 4,
                       imageWidth, imageHeight);

    QImage okArgb((uchar*)_rgbBuffer, imageWidth, imageHeight, QImage::Format_ARGB32);

   // emit vpxDecoderOkARGBImage(pox,poy,okArgb);
    m_pixmap = QPixmap::fromImage(okArgb);
    if(lastMap.isNull()){
        lastMap = QPixmap::fromImage(okArgb);
    }else{
        QPainter p(&lastMap);
        p.setCompositionMode(QPainter::CompositionMode_SourceOver);
        p.drawPixmap(pox,poy,m_pixmap);
        p.end();
    }
    update();

}

void VPxDecoder::paintEvent(QPaintEvent *){
    QPainter painter(this);
    painter.drawPixmap(0,0,this->width(),this->height(),lastMap);
}
